package juc.day01;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wang hao
 * @date 2023-02-23 20:30
 *
 *      自定义 线程池
 */
public class Demo02 {

    public static void main(String[] args) {
        int cps = 1;
        int mps = 2;
        int c = 5;
        int size = mps +  c;
        
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1, // 核心线程数量
                2, // 最大线程数量   最大线程数量 >= 核心线程数量
                60, // 空闲线程最大存活时间
                TimeUnit.SECONDS,  // 单位
                new ArrayBlockingQueue<>(5), // 任务队列
                Executors.defaultThreadFactory(),  // 创建线程工厂
                new ThreadPoolExecutor.AbortPolicy() // 任务的拒绝策略
        );

        for (int i = 0; i < size; i++) {
            threadPoolExecutor.execute(() -> {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        threadPoolExecutor.execute(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        System.out.println("wait");

    }

}
